package com.yanqu.road.server.gameplayer.module.cookboy;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.cookboy.*;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.*;
import com.yanqu.road.entity.enums.activity.eActivityCookBoyType;
import com.yanqu.road.entity.task.args.RelationActivityUnionRiseRankArgs;
import com.yanqu.road.entity.task.args.UnionRiseRankArgs;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.cookboy.CookBoyProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityMallModule;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.manger.activity.RelationActivityMgr;
import com.yanqu.road.server.manger.activity.cookboy.CookBoyActivityMgr;
import com.yanqu.road.server.manger.activity.cookboy.CookBoyMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.CookBoyPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CookBoyModule extends GeneralModule {

    public CookBoyModule(GamePlayer player) {
        super(player);
    }

    //活动id 用户数据
    private Map<Integer,CookBoyData> cookBoyDataMap = new ConcurrentHashMap<>();

    //结算了没有转换道具积分的库里把数据拉出来转换一下
    private Map<Integer,CookBoyData> cookBoyHistoryDataMap = new ConcurrentHashMap<>();


    //第三阶段的礼包只有有资格参赛的才能买
    public boolean canPrecharge3(int mallId){
        MallInfo mallInfo = RelationActivityMgr.getMallInfo(mallId);
        if(mallInfo == null){
            return false;
        }
        if(!cookBoyDataMap.containsKey(mallInfo.getActivityId())){
            return false;
        }
        if (player.getModule(ActivityMallModule.class).isOverLimitBuyCount(mallInfo, 1)) {
            //限购
            return false;
        }
        if("1".equals(mallInfo.getCanBuyParam())){
            return cookBoyDataMap.get(mallInfo.getActivityId()).isAble();
        }
        return true;
    }

    public boolean canPrecharge2(int mallId){
        MallInfo mallInfo = RelationActivityMgr.getMallInfo(mallId);
        if(mallInfo == null){
            return false;
        }
        if(!cookBoyDataMap.containsKey(mallInfo.getActivityId())){
            return false;
        }
        if (player.getModule(ActivityMallModule.class).isOverLimitBuyCount(mallInfo, 1)) {
            //限购
            return false;
        }
        if("1".equals(mallInfo.getCanBuyParam())){
            return cookBoyDataMap.get(mallInfo.getActivityId()).isAble();
        }
        return true;
    }

    public int syncSelfBanquetTable(int activityId){
        CookBoyProto.CookBoy3SelfDataSyncMsg.Builder syncMsg = CookBoyProto.CookBoy3SelfDataSyncMsg.newBuilder();
        if(cookBoyDataMap.containsKey(activityId)){
            CookBoyData cookBoyData = cookBoyDataMap.get(activityId);
            if(cookBoyData.getNo() != eActivityCookBoyType.Process3.getChildType()){
                return GameErrorCode.E_ACTIVITY_NO_FOUND;
            }
            syncMsg = CookBoyPb.parseSelfBanquetTableSync(cookBoyData);
            player.sendPacket(Protocol.U_COOK_BOY3_DATA_SYNC, syncMsg);
            return 0;
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }


    /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓小当家2↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
    public int syncSelfDishTable(int activityId,String reward){
        CookBoyProto.CookBoy2SelfDishSyncMsg.Builder syncMsg = CookBoyProto.CookBoy2SelfDishSyncMsg.newBuilder();
        if(cookBoyDataMap.containsKey(activityId)){
            CookBoyData cookBoyData = cookBoyDataMap.get(activityId);
            if(cookBoyData.getNo() != eActivityCookBoyType.Process2.getChildType()){
                return GameErrorCode.E_ACTIVITY_NO_FOUND;
            }
            syncMsg = CookBoyPb.parseSelfDishTableSync(cookBoyData);
            syncMsg.setReward(reward);
            player.sendPacket(Protocol.U_COOK_BOY2_SYNC_DISH_TABLE, syncMsg);
            return 0;
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }


    public boolean isAble(int activityId){
        if(!cookBoyDataMap.containsKey(activityId)){
            return false;
        }
        return cookBoyDataMap.get(activityId).isAble();
    }

    //获取自己的挑菜状态等信息
    public int getSelfDish(int activityId){
        CookBoyProto.CookBoy2SelfDishRespMsg.Builder syncMsg = CookBoyProto.CookBoy2SelfDishRespMsg.newBuilder();
        if(cookBoyDataMap.containsKey(activityId)){
            CookBoyData cookBoyData = cookBoyDataMap.get(activityId);
            if(cookBoyData.getNo() != eActivityCookBoyType.Process2.getChildType()){
                return GameErrorCode.E_ACTIVITY_NO_FOUND;
            }
            syncMsg = CookBoyPb.parseSelfDishTableMsg(cookBoyData);
            player.sendPacket(Protocol.U_COOK_BOY2_GET_DISH_TABLE, syncMsg);
            return 0;
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }


    /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓小当家1↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

    public int serveFood(int activityId,int no,int id){
        CookBoyData cookBoyData = cookBoyDataMap.get(activityId);
        Map<Integer, CookBoyFinishFood> cookBoyFinishFoodMap = cookBoyData.getFinishFoodMap();
        if (!cookBoyFinishFoodMap.containsKey(id)) {
            return GameErrorCode.E_COOK_BOY_SERVE_FOOD_ERR;
        }
        CookBoyFinishFood cookBoyFinishFood = cookBoyFinishFoodMap.get(id);

        if (cookBoyFinishFood.getStatus() != eCookBoyFoodStatusType.NoServe.getValue()) {
            return GameErrorCode.E_COOK_BOY_SERVE_FOOD_ERR;
        }

        CookBoyProto.CrossCookBoyServeReqMsg.Builder reqMsg = CookBoyProto.CrossCookBoyServeReqMsg.newBuilder();
        reqMsg.setActivityId(activityId);
        reqMsg.setNo(no);
        CookBoyProto.CookBoyFinishFoodTempMsg.Builder tempFood = CookBoyProto.CookBoyFinishFoodTempMsg.newBuilder();
        tempFood.setId(cookBoyFinishFood.getIndex());
        tempFood.setFoodId(cookBoyFinishFood.getFoodId());
        tempFood.setFraction(cookBoyFinishFood.getFraction());
        tempFood.setStatus(cookBoyFinishFood.getStatus());
        tempFood.setNo(cookBoyFinishFood.getNo());
        tempFood.setRank(cookBoyFinishFood.getRank());
        tempFood.setTime((int)(cookBoyFinishFood.getTime()/1000));
        reqMsg.setFinishFood(tempFood);
        reqMsg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(player.getUserInfo(), GameServer.getInstance().getServerId())));
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_COOK_BOY_SERVE_FOOD, reqMsg);
        GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
        return 0;
    }

    //获取自己的菜桌
    public int syncSelfTable(int activityId){
        CookBoyProto.CookBoySelfTableSyncMsg.Builder syncMsg = CookBoyProto.CookBoySelfTableSyncMsg.newBuilder();
        if(cookBoyDataMap.containsKey(activityId)){
            CookBoyData cookBoyData = cookBoyDataMap.get(activityId);
            if(cookBoyData.getNo() != eActivityCookBoyType.Process1.getChildType()){
                return GameErrorCode.E_ACTIVITY_NO_FOUND;
            }
            syncMsg = CookBoyPb.parseSelfTableSync(cookBoyData);
            player.sendPacket(Protocol.U_COOK_BOY_SELF_TABLE_SYNC, syncMsg);
            return 0;
        }else{
            //游戏登录中出现了活动，数据还没有，重新生成了同步一下
            CookBoyData cookBoyData = CookBoyMgr.getUserCookBoyData(activityId,player);
            if(cookBoyData != null){
                cookBoyDataMap.put(activityId,cookBoyData);
                if(cookBoyData.getNo() != eActivityCookBoyType.Process1.getChildType()){
                    return GameErrorCode.E_ACTIVITY_NO_FOUND;
                }
                syncMsg = CookBoyPb.parseSelfTableSync(cookBoyData);
                player.sendPacket(Protocol.U_COOK_BOY_SELF_TABLE_SYNC, syncMsg);
                return 0;
            }
        }
        return GameErrorCode.E_ACTIVITY_NO_FOUND;
    }


    //获取跨服的参赛菜桌
    public int syncServeTable(int activityId){
        //请求跨服桌子
        //请求的时候要加上是不是进去场景的
        CookBoyProto.CrossCookBoyTableReqMsg.Builder reqMsg = CookBoyProto.CrossCookBoyTableReqMsg.newBuilder();
        reqMsg.setActivityId(activityId);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_COOK_BOY_FOOD_TABLE_SYNC, reqMsg);
        GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
        return 0;
    }


    public void initCookBoySystem() {
        loginSendMsg();
    }

    //设置一次每个活动都不重置，禁用的试吃汤勺
    public List<Integer> getForbidSpoonIdList(int activityId){
        List<Integer> forbidSpoonIdList = new ArrayList<>();
        List<Integer> spoonIdList = CookBoyActivityMgr.getCookBoy2SpoonIdList(activityId);
        long value = player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.CookBoy2ForbidSpoon);
        for(int i = 0; i < spoonIdList.size() ; i++){
            //判断一下对应的那位是不是1
            if(((value & (1<<i))>>i) == 1){
                forbidSpoonIdList.add(spoonIdList.get(i));
            }
        }
        return forbidSpoonIdList;
    }

    //每次设置保存一下在用户身上先
    public void setForbidSpoonIdList(int activityId,List<Integer> forbidSpoonIdList){
        List<Integer> spoonIdList = CookBoyActivityMgr.getCookBoy2SpoonIdList(activityId);
        for(Integer forbidSpoonId : forbidSpoonIdList){
            if(!spoonIdList.contains(forbidSpoonId)){
                return;
            }
        }
        long value = 0;
        for(Integer forbidSpoonId : forbidSpoonIdList){
            int index = spoonIdList.indexOf(forbidSpoonId);
            value = value | (1<<index);
        }
        player.getModule(AttributeModule.class).setAttribute(ePlayerAttrType.CookBoy2ForbidSpoon,value);
    }

    public void checkCookBoy2Joiner(){
        List<ActivityInfo> activityInfoList = CookBoyActivityMgr.getOpenActivityInfoList(eActivityType.CookingBoyActivity.getValue(),eActivityCookBoyType.Process2.getChildType());
        if(activityInfoList.size() > 0){
            for(ActivityInfo activityInfo : activityInfoList){
                CookBoyData cookBoyData = cookBoyDataMap.get(activityInfo.getActivityId());
                if(cookBoyData != null && cookBoyData.getNo() == eActivityCookBoyType.Process2.getChildType() && cookBoyData.isAble()){
                    CookBoyProto.CrossCookBoy2CookerJoinSyncMsg.Builder reqMsg = CookBoyProto.CrossCookBoy2CookerJoinSyncMsg.newBuilder();
                    reqMsg.setActivityId(activityInfo.getActivityId());
                    CookBoyProto.CrossCookBoyWindUpCookerTempMsg.Builder userMsg = CookBoyProto.CrossCookBoyWindUpCookerTempMsg.newBuilder();
                    userMsg.setUserId(player.getUserId());
                    userMsg.setScore(0);//不知道哪个前面的活动晋级的
                    userMsg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(player.getUserInfo(), GameServer.getInstance().getServerId())));
                    reqMsg.addUserInfoList(userMsg);
                    YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_COOK_BOY2_COOKER_JOIN, reqMsg);
                    GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
                }
            }
        }
    }

    public void checkCookBoy3Joiner(){
        List<ActivityInfo> activityInfoList = CookBoyActivityMgr.getOpenActivityInfoList(eActivityType.CookingBoyActivity.getValue(),eActivityCookBoyType.Process3.getChildType());
        if(activityInfoList.size() > 0){
            for(ActivityInfo activityInfo : activityInfoList){
                CookBoyData cookBoyData = cookBoyDataMap.get(activityInfo.getActivityId());
                if(cookBoyData != null && cookBoyData.getNo() == eActivityCookBoyType.Process3.getChildType() && cookBoyData.isAble()){
                    CookBoyProto.CrossCookBoy3CookerJoinSyncMsg.Builder reqMsg = CookBoyProto.CrossCookBoy3CookerJoinSyncMsg.newBuilder();
                    reqMsg.setActivityId(activityInfo.getActivityId());
                    CookBoyProto.CrossCookBoyWindUpCookerTempMsg.Builder userMsg = CookBoyProto.CrossCookBoyWindUpCookerTempMsg.newBuilder();
                    userMsg.setUserId(player.getUserId());
                    userMsg.setScore(0);//不知道哪个前面的活动晋级的
                    userMsg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(player.getUserInfo(), GameServer.getInstance().getServerId())));
                    reqMsg.addUserInfoList(userMsg);
                    YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_COOK_BOY3_COOKER_JOIN, reqMsg);
                    GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
                }
            }
        }
    }

    @Override
    public void loginSendMsg() {
        //第一个阶段直接下发
        if(SystemOpenMgr.systemOpen(player, eSystemId.CookBoyActivity1.getValue())) {
            List<ActivityInfo> activityInfoList = CookBoyActivityMgr.getOpenActivityInfoList(eActivityType.CookingBoyActivity.getValue(),eActivityCookBoyType.Process1.getChildType());
            if(activityInfoList.size() > 0){
                for(ActivityInfo activityInfo : activityInfoList){
                    syncSelfTable(activityInfo.getActivityId());
                }
            }
        }
        if(SystemOpenMgr.systemOpen(player, eSystemId.CookBoyActivity2.getValue())) {
            List<ActivityInfo> activityInfoList = CookBoyActivityMgr.getOpenActivityInfoList(eActivityType.CookingBoyActivity.getValue(),eActivityCookBoyType.Process2.getChildType());
            if(activityInfoList.size() > 0){
                for(ActivityInfo activityInfo : activityInfoList){
                    syncSelfDishTable(activityInfo.getActivityId(),"");
                }
            }
        }
        if(SystemOpenMgr.systemOpen(player, eSystemId.CookBoyActivity3.getValue())) {
            List<ActivityInfo> activityInfoList = CookBoyActivityMgr.getOpenActivityInfoList(eActivityType.CookingBoyActivity.getValue(),eActivityCookBoyType.Process3.getChildType());
            if(activityInfoList.size() > 0){
                for(ActivityInfo activityInfo : activityInfoList){
                    syncSelfBanquetTable(activityInfo.getActivityId());
                }
            }
        }
        checkCookBoy2Joiner();
        checkCookBoy3Joiner();
        sendLoginSync();


        //设置汤勺的
        if(SystemOpenMgr.systemOpen(player, eSystemId.CookBoyActivity2.getValue())) {
            List<ActivityInfo> activityInfoList = CookBoyActivityMgr.getOpenActivityInfoList(eActivityType.CookingBoyActivity.getValue(),eActivityCookBoyType.Process2.getChildType());
            if(activityInfoList.size() > 0){
                for(ActivityInfo activityInfo : activityInfoList){
                    //每次登陆都去设置一下
                    sendForbidSpoonIdSet(activityInfo.getActivityId());
                }
            }
        }
        List<ActivityInfo> activityInfoList = CookBoyActivityMgr.getOpenActivityInfoList(eActivityType.CookingBoyActivity.getValue(),eActivityCookBoyType.Process1.getChildType());
        for(ActivityInfo activityInfo : activityInfoList){
            int activityId = activityInfo.getActivityId();
            //同步一下最新的区服排行数据，可能没变化
            BigInteger value = RelationActivityMgr.getUserActivityConditionValue(activityId, eGamePlayerEventType.CookBoy1IntegralServerCrossRank.getValue());
            RelationActivityMgr.conditionNotify(activityId,player.getUserId(), eGamePlayerEventType.CookBoy1IntegralServerCrossRank.getValue(),
                    new RelationActivityUnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_NORMAL, value,"", activityId));
        }

    }

    public void sendForbidSpoonIdSet(int activityId){
        List<Integer> spoonIdList = getForbidSpoonIdList(activityId);
        CookBoyMgr.sendForbidSpoon(activityId,player.getUserId(),spoonIdList);
    }

    public void sendLoginSync(){
        List<ActivityInfo> activityInfoList = CookBoyActivityMgr.getOpenActivityInfoList(eActivityType.CookingBoyActivity.getValue(),eActivityCookBoyType.Process3.getChildType());
        if(activityInfoList.size() > 0){
            for(ActivityInfo activityInfo : activityInfoList){
                CookBoyData cookBoyData = cookBoyDataMap.get(activityInfo.getActivityId());
                if(cookBoyData != null && cookBoyData.getNo() == eActivityCookBoyType.Process3.getChildType()){
                    CookBoyProto.CrossCookBoy3LoginSyncMsg.Builder reqMsg = CookBoyProto.CrossCookBoy3LoginSyncMsg.newBuilder();
                    reqMsg.setActivityId(activityInfo.getActivityId());
                    reqMsg.setUserId(player.getUserId());
                    YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_COOK_BOY3_USER_LOGIN, reqMsg);
                    GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
                }
            }
        }
    }

    //策划要测试做10000道各种菜多少分
    public void test(){
        long userId = player.getUserId();
        if(userId != 888810000000001L){
            return;
        }
        int activityId = 9986901;
        CookBoyData cookBoyData = new CookBoyData();
        cookBoyData.setActivityId(activityId);
        cookBoyData.setUserId(userId);
        cookBoyData.setNo(1);
        cookBoyData.setAble(true);
        cookBoyData.setIntegral(0);
        cookBoyData.setAllIntegral(0);
        cookBoyData.setHp(CookBoyActivityMgr.getCookBoyHp(activityId));
        cookBoyData.setHpTime(System.currentTimeMillis());
        cookBoyData.setFinishFoodId(CookBoyActivityMgr.getDefaultCookingId(activityId));
        cookBoyData.setTableFoodMap(initCookBoyFood());
        cookBoyData.setFinishFoodCount(0);
        cookBoyData.setFinishFoodData("{}");
        cookBoyData.setDishId(1);
        cookBoyData.setProcess(0);
        cookBoyData.setDishCount(0);

        Map<Integer,Integer> countMap = new ConcurrentHashMap<>();
        String res = "";
        Map<Integer, CookBoyFoodConfig> foodList = CookBoyActivityMgr.getActivityFoodConfigMap(activityId);
        for(CookBoyFoodConfig cookBoyFoodConfig : foodList.values()){
            for(int i = 100000;i > 0; i--){
                makeFoodTest(cookBoyData,15);
                CookBoyFinishFood cookBoyFinishFood = foodMergeTest(cookBoyData,cookBoyFoodConfig.getCookingId());
                if(cookBoyFinishFood != null){
                    if(!countMap.containsKey(cookBoyFoodConfig.getCookingId())){
                        countMap.put(cookBoyFoodConfig.getCookingId(),0);
                    }
                    countMap.put(cookBoyFoodConfig.getCookingId(),countMap.get(cookBoyFoodConfig.getCookingId())+1);
                    res += cookBoyFinishFood.getFoodId()+"="+cookBoyFinishFood.getFraction()+";";
                    if(countMap.get(cookBoyFoodConfig.getCookingId()) > 10000){break;}
                }
            }
        }
        log.error(res);
    }

    private void makeOneFood(CookBoyFood cookBoyFood,int activityId){
        int level = CookBoyActivityMgr.randomOneFoodLevel(activityId);
        Map<Integer,Integer> scoreMap = CookBoyActivityMgr.getMakeFoodScoreMap(activityId);
        cookBoyFood.setLevel(level);
        cookBoyFood.setFraction(scoreMap.get(level));
    }

    private int getEmptyTableCount(Map<Integer,CookBoyFood> foodMap){
        int count = 0;
        for(int i = 1; i <= GameConfig.COOK_BOY_TABLE_MAX_POSITION; i++){
            CookBoyFood tempFood = foodMap.get(i);
            if(tempFood.getLevel() == 0){
                count++;
            }
        }
        return count;
    }

    public MakeFoodRes makeFoodTest(CookBoyData cookBoyData, int times){
        MakeFoodRes res = new MakeFoodRes();
        Map<Integer,CookBoyFood> foodMap = cookBoyData.getTableFoodMap();

        //次数大于剩余的空位置是修正到满
        int emptyCount = getEmptyTableCount(foodMap);
        if(times > getEmptyTableCount(foodMap)){
            times = emptyCount;
        }
        cookBoyData.setHp(cookBoyData.getHp() - times);
        for(int i = 1; i <= GameConfig.COOK_BOY_TABLE_MAX_POSITION; i++){
            CookBoyFood tempFood = foodMap.get(i);
            if(tempFood.getLevel() == 0){
                makeOneFood(tempFood,cookBoyData.getActivityId());
                res.addCookBoyFood(tempFood);
                if(--times == 0){
                    break;
                }
            }
        }
        cookBoyData.setTableFoodMap(foodMap);
        return res;
    }

    public CookBoyFinishFood foodMergeTest(CookBoyData cookBoyData,int foodId){
        CookBoyFinishFood newCookBoyFinishFood = null;
        long addIntegral = 0;
        if(cookBoyData == null){
            return null;
        }
        Map<Integer, CookBoyFood> foodMap = cookBoyData.getTableFoodMap();
        Map<Integer,CookBoyFinishFood> finishFoodMap = cookBoyData.getFinishFoodMap();
        //菜没有切满,最后一个地方没有菜
        if(foodMap.get(foodMap.size()).getLevel() == 0){
            return null;
        }
        Map<Integer,List<CookBoyFood>> levelMap = new ConcurrentHashMap<>();
        for(int level = 1; level <= GameConfig.COOK_BOY_FOOD_MAX_LEVEL; level++){
            levelMap.put(level,new ArrayList<>());
        }
        for(int pos = 1; pos <= GameConfig.COOK_BOY_TABLE_MAX_POSITION; pos++){
            CookBoyFood tempFood = foodMap.get(pos);
            if(tempFood.getLevel() == 0){
                break;
            }
            levelMap.get(tempFood.getLevel()).add(tempFood);
        }

        for(int level = 1; level <= GameConfig.COOK_BOY_FOOD_MAX_LEVEL;level++){
            List<CookBoyFood> cookBoyFoodList = levelMap.get(level);
            if(cookBoyFoodList.size() <= 1)continue;
            long allScore = 0;
            int combe = cookBoyFoodList.size() - 1;
            int mulValue;
            if(combe > 1){
                mulValue = CookBoyActivityMgr.getMergeFoodCombe(cookBoyData.getActivityId(),level,combe);
            }else{
                mulValue = 1000;
            }
            for(CookBoyFood cookBoyFood : cookBoyFoodList){
                allScore += cookBoyFood.getFraction();
            }
            levelMap.put(level,new ArrayList<>());
            //等级大于合成菜变成成品菜
            if(level + 1 > GameConfig.COOK_BOY_FOOD_MAX_LEVEL){
                newCookBoyFinishFood = new CookBoyFinishFood();
                int addScore = CookBoyActivityMgr.getCookBoyFinishFoodAddScore(cookBoyData.getActivityId(),foodId);
                newCookBoyFinishFood.setFoodId(foodId);
                newCookBoyFinishFood.setIndex(finishFoodMap.size()+1);
                newCookBoyFinishFood.setFraction((int)((allScore * mulValue * (1000 + addScore)) / (1000 * 1000)));
                newCookBoyFinishFood.setNo(0);
                newCookBoyFinishFood.setRank(0);
                newCookBoyFinishFood.setTime(System.currentTimeMillis());
                newCookBoyFinishFood.setStatus(eCookBoyFoodStatusType.NoServe.getValue());
                newCookBoyFinishFood.setLevel(level + 1);
                newCookBoyFinishFood.makeProcess(cookBoyFoodList);
                finishFoodMap.put(finishFoodMap.size()+1,newCookBoyFinishFood);
                cookBoyData.setFinishFoodMap(finishFoodMap);
                addIntegral += newCookBoyFinishFood.getFraction();
            }else{
                CookBoyFood newCookBoyFood = new CookBoyFood();
                newCookBoyFood.setLevel(level + 1);
                newCookBoyFood.setFraction((int)(allScore * mulValue / 1000));
                newCookBoyFood.makeProcess(cookBoyFoodList);
                levelMap.get(level+1).add(newCookBoyFood);
                addIntegral += newCookBoyFood.getFraction();
            }
        }
        Map<Integer,CookBoyFood> newTable = initCookBoyFood();
        for(int level = 1; level <= GameConfig.COOK_BOY_FOOD_MAX_LEVEL;level++) {
            List<CookBoyFood> cookBoyFoodList = levelMap.get(level);
            for(CookBoyFood cookBoyFood : cookBoyFoodList){
                setOneFood(newTable,cookBoyFood);
            }
        }
        cookBoyData.setTableFoodMap(newTable);
        cookBoyData.setAllIntegral(cookBoyData.getAllIntegral() + addIntegral);
        return newCookBoyFinishFood;
    }

    private Map<Integer, CookBoyFood> initCookBoyFood(){
        Map<Integer,CookBoyFood> cookBoyFoodMap = new ConcurrentHashMap<>();
        for(int i = 1; i <= GameConfig.COOK_BOY_TABLE_MAX_POSITION; i++){
            CookBoyFood temp = new CookBoyFood();
            temp.setPos(i);
            temp.setLevel(0);
            temp.setFraction(0);
            temp.setProcess("");
            cookBoyFoodMap.put(temp.getPos(),temp);
        }
        return cookBoyFoodMap;
    }

    private void setOneFood(Map<Integer,CookBoyFood> foodTable,CookBoyFood cookBoyFood){
        for(CookBoyFood temp : foodTable.values()){
            if(temp.getLevel() == 0){
                temp.setLevel(cookBoyFood.getLevel());
                temp.setFraction(cookBoyFood.getFraction());
                temp.setProcess(cookBoyFood.getProcess());
                break;
            }
        }
    }


    @Override
    public boolean loadData() {
//        test();
        List<ActivityInfo> activityInfoList = CookBoyActivityMgr.getOpenActivityInfoList(eActivityType.CookingBoyActivity.getValue());
        if(activityInfoList.size() > 0){
            for(ActivityInfo activityInfo : activityInfoList){
                CookBoyData cookBoyData = null;
                if(activityInfo.getChildType() == eActivityCookBoyType.Process1.getChildType()){
                    cookBoyData = CookBoyMgr.getUserCookBoyData(activityInfo.getActivityId(),player);
                }else if(activityInfo.getChildType() == eActivityCookBoyType.Process2.getChildType()){
                    cookBoyData = CookBoyMgr.getUserCookBoy2DataNoLock(activityInfo.getActivityId(),player);
                }else if(activityInfo.getChildType() == eActivityCookBoyType.Process3.getChildType()){
                    cookBoyData = CookBoyMgr.getUserCookBoy3Data(activityInfo.getActivityId(),player);
                }
                if(cookBoyData != null){
                    cookBoyDataMap.put(activityInfo.getActivityId(),cookBoyData);
                }
            }
        }
        List<Integer> activityIdList = new ArrayList<>();
        for(ActivityInfo activityInfo : activityInfoList){
            activityIdList.add(activityInfo.getActivityId());
        }
        //回收道具，已经改成只有领奖期才回收了，后面先留着万一要改
        /*cookBoyHistoryDataMap = UserCookBoyBussiness.getUserHistoryRecoverCookBoy2Map(getUserId(),activityIdList);
        for(CookBoyData cookBoyData : cookBoyHistoryDataMap.values()){
            int integralItemId = CookBoyActivityMgr.getCookBoy2IntegralItemId(cookBoyData.getActivityId());
            String reward = integralItemId + "=" + cookBoyData.getIntegral();
            player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.CookBoy,eLogMoneyType.CrossCookBoy2ActivityHistoryReward);
            cookBoyData.setIntegral(0);
        }
        if(cookBoyHistoryDataMap.size() > 0){
            List<CookBoyData> updateList = new ArrayList<>(cookBoyHistoryDataMap.values());
            UserCookBoyBussiness.updateUserCookBoyList(updateList);
        }*/
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        if(SystemOpenMgr.systemOpen(player, eSystemId.CookBoyActivity1.getValue())) {
            List<ActivityInfo> activityInfoList = CookBoyActivityMgr.getOpenActivityInfoList(eActivityType.CookingBoyActivity.getValue(),eActivityCookBoyType.Process1.getChildType());
            if(activityInfoList.size() > 0){
                for(ActivityInfo activityInfo : activityInfoList){
                    int activityId = activityInfo.getActivityId();
                    CookBoyProto.CookBoySelfTableSyncMsg.Builder syncMsg = CookBoyProto.CookBoySelfTableSyncMsg.newBuilder();
                    if(cookBoyDataMap.containsKey(activityId)){
                        CookBoyData cookBoyData = cookBoyDataMap.get(activityId);
                        if(cookBoyData.getNo() != eActivityCookBoyType.Process1.getChildType()){
                            continue;
                        }
                        int oldHp = cookBoyData.getHp();
                        CookBoyMgr.recoverHp(activityId, getUserId(), 0);
                        int nowHp = cookBoyData.getHp();
                        if (nowHp != oldHp) {
                            syncMsg = CookBoyPb.parseSelfTableSync(cookBoyData);
                            player.sendPacket(Protocol.U_COOK_BOY_SELF_TABLE_SYNC, syncMsg);
                        }
                    }else{
                        if(activityInfo.getChildType() == eActivityCookBoyType.Process1.getChildType()){
                            CookBoyData cookBoyData = CookBoyMgr.getUserCookBoyData(activityInfo.getActivityId(),player);
                            if(cookBoyData != null){
                                cookBoyDataMap.put(activityInfo.getActivityId(),cookBoyData);
                                syncSelfTable(activityId);
                            }
                        }
                        if(activityInfo.getChildType() == eActivityCookBoyType.Process2.getChildType()){
                            CookBoyData cookBoyData = CookBoyMgr.getUserCookBoy2DataNoLock(activityInfo.getActivityId(),player);
                            if(cookBoyData != null){
                                cookBoyDataMap.put(activityInfo.getActivityId(),cookBoyData);
                            }
                        }
                    }
                }
            }
        }
        return true;
    }
}
